<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>

<body>
    <div class="container">
        <input id="document" accept="application/vnd.openxmlformats-officedocument.wordprocessingml.document" type="file" />
        <div class="row" style="width: 100%;">
            <div class="span8">
                <div id="output" class="well"></div>
            </div>
        </div>
    </div>
    <script src="https://cdn.bootcss.com/mammoth/1.4.8/mammoth.browser.js"></script>
    <script type="text/javascript">
        window.addEventListener('message', (e) => {
            selectDocFile();
        });

        function selectDocFile() {
            document.getElementById("document").click();
        }

        document.getElementById("document")
            .addEventListener("change", readFileInputEventAsArrayBuffer, true);

        function displayResult(result) {
            let html = result.value;
            let newHTML = html.replace(//g, ' ')
                .replace('<h1>', '<h1 style="text-align: center;">')
                .replace(/<table>/g, '<table style="border-collapse: collapse;">')
                .replace(/<tr>/g, '<tr style="height: 30px;">')
                .replace(/<td>/g, '<td style="border: 1px solid pink;">')
                .replace(/<p>/g, '<p style="font-size: 16px;text-indent: 2em;">')
                .replace(/<p style>/g, '<p style="font-size: 16px;text-indent: 2em;">');
            const data = {
                newHTML
            }
            this.parent.postMessage(data, '*');
        }


        function readFileInputEventAsArrayBuffer(event) {
            var file = event.target.files[0];
            var reader = new FileReader();

            reader.onload = function(loadEvent) {
                var arrayBuffer = loadEvent.target.result;
                mammoth.convertToHtml({
                        arrayBuffer: arrayBuffer
                    })
                    .then(displayResult)
                    .done();
            };
            reader.readAsArrayBuffer(file);
        }
    </script>
</body>

</html>